package su.Ner;
import iitb.CRF.DataIter;
import iitb.CRF.DataSequence;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class NerDataIter implements DataIter {

	BufferedReader br;
	String currentLine;
	String filePath;
	static public int maxLength;
	int i;
	
	public NerDataIter(String _filePath,int _maxLength) throws IOException{
		filePath=_filePath;
		maxLength=_maxLength;
	}
	public boolean hasNext() {
		// TODO Auto-generated method stub
		if (!(currentLine==null) && !currentLine.equals("") && i<maxLength)
		return true;
		try {
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		return false;
	}

	public DataSequence next() {
		String res=currentLine;
		try {
			currentLine=br.readLine();
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(-1);
		}
		i++;
		return new NerDataSequence(res);
		
	}

	public void startScan() {
		i=0;
		try{
			br = new BufferedReader(
			         new FileReader(new File(filePath)));
			//br.read();
			currentLine=br.readLine();
		}catch (Exception e){
			e.printStackTrace();
			System.exit(-1);
		}
		
	}
	
	public static void main(String[] args) throws Exception {		
		NerDataIter.cleanDataSets(".\\ner\\data\\train.txt");
		NerDataIter.cleanDataSets(".\\ner\\data\\test.txt");
	}

	public static void cleanDataSets(String file) throws Exception{
		BufferedWriter bw=new BufferedWriter(new FileWriter(file+"1"));
		NerDataIter r=new NerDataIter(file,Integer.MAX_VALUE);
		for(r.startScan();r.hasNext();){
			DataSequence d=r.next();
			boolean good=true;
			String res=d.toString()+"\n";
			for(int i=0;i<d.length();i++){
				if(((String)d.x(i)).length()==0)
					good=false;
			}
			if(!((NerDataSequence)d).good) 
				good =false;
			if(good)
				bw.write(res);
		}
		bw.close();
	}
}
